<template>
	<view class="page">
		<image src="/static/logo.jpg"	mode="widthFix"></image>

		<view class="input">
			<input type="number" placeholder="请输入手机号" maxlength="11" v-model="username" />
		</view>
		<view class="input">
			<input type="password" placeholder="请输入登录密码" v-model="password" />
		</view>
		<view class="input">
			<input type="text" placeholder="请输入昵称" v-model="nickname" />
		</view>

		<view class="btn" @tap="login">
			注册
		</view>

	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import graceChecker from '@/common/graceChecker';
	export default {
		data() {
			return {
				url:null,
				username: '',
				password: '',
				nickname: ''
			}
		},
		computed: {
			...mapState(['user', 'common'])
		},
		onLoad(option) {
			if(option.url) {
				this.url = decodeURIComponent(option.url)
			}
		},
		methods: {
			async login() {
				let formData = {
					username: this.username,
					password: this.password,
					nickname: this.nickname
				}
				var rule = [
					{name: 'username',checkType: 'phone',errorMsg: '请输入手机号'},
					{name: 'password',checkType: 'notnull',errorMsg: '请输入登录密码'}
				];
				var checkRes = graceChecker.check(formData, rule);
				if (checkRes) {
					let res = await this.$api.register(formData);
					if(res.code == 1) {
						let data = res.data
						this.$wanlshop.msg('注册成功');
						this.$store.dispatch('user/login', data.userinfo);
						this.$store.dispatch('user/setToken', data.userinfo.token);
						if (!this.common.store) {
							uni.reLaunch({
								url: '/pages/user/store/store'
							})
						} else {
							setTimeout(() => {
								if(this.url) {
									if (this.url == '/pages/home' || this.url == '/pages/user') {
										uni.switchTab({
											url: this.url
										});
									} else {
										uni.redirectTo({
											url:this.url
										})
									}
								} else {
									this.$wanlshop.on('/pages/home')
								}
							}, 1500);
						}
					}
				} else {
					this.$wanlshop.msg(graceChecker.error);
				}
			}
		}
	}
</script>
<style lang="less">
	page {
		background-color: #fff;
	}
</style>
<style lang="less" scoped>
	.page {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 100rpx;

		>image {
			border-radius: 50%;
			width: 160rpx;
			height: 160rpx;
			margin-bottom: 100rpx;
		}

		.input {
			box-shadow: 0 0 30px 0 rgba(43, 86, 112, .1);
			border-radius: 50rpx;
			width: 690rpx;
			height: 100rpx;
			padding: 0 30rpx;
			margin: 30rpx 0;

			input {
				height: 100%;
				font-size: 30rpx;
			}
		}

		.btn {
			width: 510rpx;
			height: 84rpx;
			background: #FA802A;
			border-radius: 42rpx;
			font-size: 34rpx;
			color: #fff;
			line-height: 84rpx;
			text-align: center;
			margin-top: 200rpx;
		}
	}
</style>